/**   
 * @Title: UserAction.java 
 * @Package com.foreveross.smsh.models.user.web 
 * @Description: TODO 
 * @author lixiaodong lixiaodong@foreveross.com  
 * @date 2016年3月4日 下午3:30:52 
 * @version V1.0   
 */
package com.foreveross.smsh.models.user.web;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.smsh.core.web.ModelAction;
import com.foreveross.smsh.models.user.pojo.User;
import com.foreveross.smsh.models.user.service.UserService;

/** 
 * @ClassName: UserAction 
 * @Description: TODO
 * @author A18ccms a18ccms_gmail_com 
 * @date 2016年3月4日 下午3:30:52 
 *  
 */
@Controller
@RequestMapping("/user")
//@SessionAttributes(value = {"user"})
public class UserAction  extends ModelAction<User>{
	private static final Logger logger = LogManager.getLogger(UserAction.class);
	@Autowired
	private UserService userService;
	
	@RequestMapping(method=RequestMethod.GET,produces= "text/plain;charset=UTF-8")
	public String list(){
		return "models/user/user-list";
	}
	@RequestMapping(value="/query",method=RequestMethod.POST)
	@ResponseBody
	public void  query(User user){
			userService.queryUser(getPaginator());
	this.printJson(getPaginator());
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(){
		return "models/user/user-update";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public void doAdd(User user){
		user.setCreatedate(new Date());
		userService.add(user);
		logger.info("增加用户成功！");
		this.printJson("true");
	}
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public String update(@ModelAttribute User user,Model model){
		model.addAttribute("user", user);
	//	attr("user", user);
		return "models/user/user-update";
	}
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public void doUpdate(@ModelAttribute User user){
		user.setCreatedate(new Date());
		userService.update(user);
		logger.info("修改用户成功！");
		this.printJson("true");
	}
	
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	@ResponseBody
	public void remove(String id){
		userService.delete(id);
		logger.info("删除用户成功！");
		this.printJson("true");
	}
	
	@ModelAttribute
	public void getUser(User user,Model model){
		if(StringUtils.isNotBlank(user.getId())){
			user =userService.get(user.getId());
			model.addAttribute("user", user);
		}else{
			model.addAttribute("user", user);
		}
	}
	
}
